//
//  DramerPersonViewController.m
//  SmileRead
//
//  Created by dllo on 15/8/26.
//  Copyright (c) 2015年 JIZ. All rights reserved.
//

#import "DramerPersonViewController.h"
#import "Path.h"
#import "MyImageView.h"
#import "FileNameList.h"

@interface DramerPersonViewController ()<UITextFieldDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (nonatomic, retain) MyImageView *personImage;

@property (nonatomic, retain) UITextField *nameText;

@property (nonatomic, retain) UITextField *mottoText;

@property (nonatomic, retain) UIButton *backBut;

@property (nonatomic, retain) UIImagePickerController *picker;

// 确定按钮
@property (nonatomic, retain) UIButton *button;

@property (nonatomic, retain) NSUserDefaults *userInfo;

@property (nonatomic, retain) NSString *personName;

@property (nonatomic, retain) NSData *imageData;

@end

@implementation DramerPersonViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        // 初始化
        self.userInfo = [NSUserDefaults standardUserDefaults];
        
        self.imageData = [NSData data];
    }
    return self;
}

- (void)dealloc
{
    [_personName release];
    [_userInfo release];
    [_picker release];
    [_mottoText release];
    [_backBut release];
    [_personImage release];
    [_nameText release];
    [super dealloc];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createSubView];
}

- (void)createSubView
{
    self.backBut = [UIButton buttonWithType:UIButtonTypeCustom];
    self.backBut.frame = CGRectMake(20 * widthFit, 60 * heightFit, 35 * widthFit, 35 * widthFit);
    [self.backBut setBackgroundImage:[UIImage imageWithContentsOfFile:[Path pathOfImageWithName:@"back.png"]] forState:UIControlStateNormal];
    [self.backBut addTarget:self action:@selector(backBut:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.backBut];
    [_backBut release];
    
    // 创建个人设置界面
    
    self.personImage = [[MyImageView alloc] initWithFrame:CGRectMake(50 * widthFit, 160 * heightFit, 80 * widthFit, 80 * widthFit)];
    
    NSString *path = [Path pathOfLibraryWithFileName:LY_Image];
    
    NSFileManager *file = [NSFileManager defaultManager];
    if ([file fileExistsAtPath:path]) {
        
        self.personImage.imageView.image = [UIImage imageWithContentsOfFile:path];
    }
    else {
         self.personImage.imageView.image = [UIImage imageNamed:[Path pathOfImageWithName:@"xiangji.png"]];
    }
   
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(person:)];
    [self.personImage addGestureRecognizer:tap];
    self.personImage.layer.cornerRadius = self.personImage.frame.size.height / 2;
    self.personImage.layer.masksToBounds = YES;
    [self.view addSubview:self.personImage];
    [_personImage release];
    
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(50 * widthFit, 270 * heightFit, 50 * widthFit, 30 * heightFit)];
    name.text = @"昵称:";
    [self.view addSubview:name];
    [name release];
    
    self.nameText = [[UITextField alloc] initWithFrame:CGRectMake(100 * widthFit, 270 * heightFit, self.view.frame.size.width - 150 * widthFit, 30 * heightFit)];
    self.nameText.backgroundColor = [Path colorWithRed:200 green:209 blue:233];
    self.nameText.borderStyle = 3;
    self.nameText.delegate = self;
    self.nameText.placeholder = @"请输入你的昵称";
    [self.view addSubview:self.nameText];
    [_nameText release];
    
    UILabel *motto = [[UILabel alloc] initWithFrame:CGRectMake(50 * widthFit, 350 * heightFit, 50 * widthFit, 30 * heightFit)];
    motto.text = @"名言";
    [self.view addSubview:motto];
    [motto release];
    
    self.mottoText = [[UITextField alloc] initWithFrame:CGRectMake(100 * widthFit, 350 * heightFit, self.view.frame.size.width - 150 * widthFit, 30 * heightFit)];
    self.mottoText.placeholder = @"请输入你喜欢的一句话";
    self.mottoText.borderStyle = 3;
    self.mottoText.delegate = self;
    //self.mottoText.backgroundColor = [UIColor grayColor];
    self.mottoText.backgroundColor = [Path colorWithRed:200 green:209 blue:233];
    [self.view addSubview:self.mottoText];
    [_mottoText release];
    
    self.button = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 50 * widthFit, 420 * heightFit, 100 * widthFit, 40 * heightFit)];
    [self.button setTitle:@"确定" forState:UIControlStateNormal];
    self.button.layer.cornerRadius = 10;
    self.button.layer.masksToBounds = YES;
    self.button.backgroundColor = [UIColor greenColor];
    [self.button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button];
    
    // 创建本地相册
    self.picker = [[[UIImagePickerController alloc] init] autorelease];
    self.picker.delegate = self;
    self.picker.allowsEditing = YES;
    
}

// 确定按钮
- (void)buttonClicked:(UIButton *)button
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"用户信息" message:@"你好" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
    }];
    
    UIAlertAction *determine = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
        // 将用户信息存到本地(NSUserDefaults 数据持久化)
         //首先遍历字典, 看是否有NSNull类型, 如果有, 就不能存到NSUserDefaults中, 必须将NSNull类型转化为@""或nil
                // 存储到本地
        if (self.personName == nil) {
            self.personName = @"";
        }
        
        [self.userInfo setObject:self.personName forKey:@"name"];
        [self.userInfo setObject:self.imageData forKey:@"image"];
        // 将数据同步到本地
        [self.userInfo synchronize];
    }];
    
    [alert addAction:cancle];
    [alert addAction:determine];
    
    [self presentViewController:alert animated:YES completion:^{
        
    }];

}

- (void)person:(UITapGestureRecognizer *)tap
{
    [self presentViewController:self.picker animated:YES completion:^{
        
    }];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    self.personImage.imageView.image = [info objectForKey:UIImagePickerControllerEditedImage];
    
    // 将图片存储到文件夹
    NSData *imageData = UIImagePNGRepresentation([info objectForKey:UIImagePickerControllerEditedImage]);
    self.imageData = imageData;
    NSString *path = [Path pathOfLibraryWithFileName:LY_Image];
    NSFileManager *file = [NSFileManager defaultManager];
    if ([file fileExistsAtPath:path]) {
        [file removeItemAtPath:path error:nil];
    }
    [imageData writeToFile:path atomically:YES];
    [self.picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField == self.nameText) {
        self.personName = textField.text;
    }
    else {
        [self.userInfo setObject:textField.text forKey:@"motto"];
    }
}

// 回收键盘
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:NO];
}


// 点击返回
- (void)backBut:(UIButton *)button
{
    [self.delegate sendWithUserInfo:self.userInfo];
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
